package org.jetnuke.modules.news;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Autonews;
import org.jetnuke.entities.Stories;

public class Category implements IAction {

	public void setCatid(int catid) {
		this.catid = catid;
	}
	int catid;


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {

		synchronized (Autonews.class) {
			//Iterator it = autonewsPeer.doSelect(
			//		new Criteria().add(autonewsPeer.TIME, new Date(),
			//				Criteria.GREATER_THAN)).iterator();
			Iterator it = DataAccessDriver.getInst().newAutoNewsDAO().selectDateGreater(new Date()).iterator();
			while (it.hasNext()) {
				Autonews news = (Autonews) it.next();
				Stories story = new Stories();
				story.setACOMM(news.getACOMM());
				story.setAID(2);//TODO FIXME  !!!!! news.getAID());
				story.setALANGUAGE(news.getALANGUAGE());
				story.setASSOCIATED(news.getASSOCIATED());
				story.setBODYTEXT(news.getBODYTEXT());
				story.setCATID(news.getCATID());
				story.setHOMETEXT(news.getHOMETEXT());
				story.setIHOME(news.getIHOME());
				story.setINFORMANT(news.getINFORMANT());
				story.setNOTES(news.getNOTES());
				story.setTITLE(news.getTITLE());
				story.setTOPIC(news.getTOPIC());
				DataAccessDriver.getInst().newStoriesDAO().addNew(story);
				//story.save();
				DataAccessDriver.getInst().newAutoNewsDAO().deleteByID(news.getANID());
				//autonewsPeer.doDelete(news);
			}
		}

		/*Criteria crt = new Criteria();

		if (new_topic == 0) {
			Criteria.Criterion a1 = crt.getNewCriterion(storiesPeer.IHOME,
					new Integer(1), Criteria.EQUAL);
			Criteria.Criterion a2 = crt.getNewCriterion(storiesPeer.CATID,
					new Integer(0), Criteria.EQUAL);
			crt.add(a1.or(a2));
		} else {
			crt.add(storiesPeer.TOPIC, new_topic);
		}
		crt.addDescendingOrderByColumn(storiesPeer.SID);
		crt.setLimit(10);//TODO
		List stories = storiesPeer.doSelect(crt);*/
		int limit=10;
		DataAccessDriver.getInst().newStoriesCatDAO().incCounter(catid);
		List stories=DataAccessDriver.getInst().newStoriesDAO().getStoriesByCategory_SidDesc(catid,limit);
		Iterator iterator = stories.iterator();
		HashMap mp = new HashMap();
		HashMap topics = new HashMap();
		vc.put("mp", mp);
		vc.put("topics", topics);
		while (iterator.hasNext()) {
			Stories story = (Stories) iterator.next();
			topics.put(new Integer(story.getSID()), DataAccessDriver.getInst().newTopicsDAO().retrieveByPK(story.getTOPIC()));
					
			float rated = 0;
			if (story.getSCORE() > 0) {
				rated=(float)(((double)story.getSCORE())/story.getRATINGS());
				
				//rated = story.getRATINGS() / story.getSCORE();
				mp.put(story, new Float(rated));
			} else {
				mp.put(story, new Float(0));
			}
		}
		vc.put("stories", stories);
		return new ActionForward(op, info);
	}

}
